package com.meiqiongproperty.yiguanjia.fragment.home.tenement;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.CommunityInformationLayoutBinding;
import com.meiqiongproperty.yiguanjia.db.utils.DBUserUtils;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.tenement.InformationAdapter;
import com.meiqiongproperty.yiguanjia.fragment.home.h5.H5UserActivity;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendCommunityInfo;
import com.meiqiongproperty.yiguanjia.httpbean.TenementInformationInfo;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.Information;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 社区资讯
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment.home.tenement
 * @User ChenJing
 * @Date 2017/4/25
 */

public class CommunityInformationFragment extends BaseFragment {
    CommunityInformationLayoutBinding binding;
    private InformationAdapter adapter;
    private Information information;
    private List<Information> informationList=new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding= DataBindingUtil.inflate(inflater, R.layout.community_information_layout,container,false);
        binding.navigation.setTitle("社区资讯");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        initView();
        return binding.getRoot();
    }

    private void initView() {
        SendCommunityInfo communityInfo=new SendCommunityInfo();
        communityInfo.setToken(DBUserUtils.getUserInfo(getActivity()).getToken());
        communityInfo.setRoomID(DBUserUtils.getmDefaultHouse(getActivity()).getID());
        communityInfo.setType("2");//  1 平台资讯 2物业资讯
        communityInfo.setPageNo("1");
        communityInfo.setPageSize("100000");
        ApiService.getInfo(getActivity(), new IHttpCallback<TenementInformationInfo>() {
            @Override
            public void onNext(TenementInformationInfo tenementInformationInfo) {
                initRecycle(tenementInformationInfo);
            }

            @Override
            public void onError(int code, String message) {
            }
        }, Utils.getRequestBody(communityInfo));
    }

    private void initRecycle(final TenementInformationInfo tenementInformationInfo) {
        for(int i=0;i<tenementInformationInfo.getInfo().size();i++){
            information=new Information();
            information.setTitle(tenementInformationInfo.getInfo().get(i).getTitle());
            information.setTime(tenementInformationInfo.getInfo().get(i).getCreateAt());
            information.setImageurl(tenementInformationInfo.getInfo().get(i).getImage());
            informationList.add(information);
        }
        adapter=new InformationAdapter(getActivity(),informationList);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.communityInformationRecycler.setLayoutManager(manager);
        binding.communityInformationRecycler.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent i = new Intent();
                i.putExtra(H5UserActivity.URL, tenementInformationInfo.getInfo().get(position).getUrl());
                i.putExtra(H5UserActivity.TITLE, tenementInformationInfo.getInfo().get(position).getTitle());
                i.setClass(getActivity(), H5UserActivity.class);
                startActivity(i);
            }
        });
    }
}
